//
//  YCZBViewController.m
//  LOLBox
//
//  Created by mac on 15-4-9.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCZBViewController.h"
#import "YCZBCell.h"
#import "YCZBModel.h"
#import "HttpRequestManager.h"

@interface YCZBViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UICollectionView *collectionView;


@end

@implementation YCZBViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self createNavTitleView:@"装备"];
        [self createBackBarBtnWithTarget:self WithAction:@selector(popTheView:)];
    }
    return self;
}

- (void)popTheView:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    [self createCollectionView];
}
- (void)getData
{
    self.dataSource = [NSMutableArray array];
    [[HttpRequestManager shareInstance] getZBInfo:^(NSMutableArray *obj) {
        //[self.dataSource arrayByAddingObjectsFromArray:obj];
        [self.dataSource addObjectsFromArray:obj];
    }];
    [self.collectionView reloadData];
}
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(70, 90);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight([[UIScreen mainScreen] bounds]) - 50 - 64) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"YCZBCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        YCZBCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        if (self.dataSource.count > 0)
        {
            YCZBModel *model = self.dataSource[indexPath.row];
            [cell fillCellWithModel:model];
        }
        
        return cell;
    
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"点击第 %d",indexPath.row);
//    YCZBModel *m = self.dataSource[indexPath.row];
//    
//    //跳转到视频列表 传值过去
//    YCVideoList *list = [[YCVideoList alloc] init];
//    
//    
//    [list senderVaule:[self clipString:_currentUrl andId:[NSString stringWithFormat:@"%@",m.Id]]];
//    [self.navigationController pushViewController:list animated:YES];
//    //NSLog(@"1");
    
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(70, 90);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    CGFloat lengFloat = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    CGFloat spLeng = (lengFloat - 70 * 3) / 5;
    return  UIEdgeInsetsMake(5, spLeng, 5, spLeng);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//字符串裁剪  /v4/videotype_4   ".json"
- (NSString *)clipString:(NSString *)str andId:(NSString *)Id;
{
    NSInteger length = [str length];
    //得到数字
    NSString *num = [str substringWithRange:NSMakeRange(length - 6, 1)];
    //NSLog(@"数字%@",num);
    
    NSString *s = [str substringToIndex:length - 16];
    //NSLog(@"字符组:%@",s);
    
    //拼接字符串
    NSString *lastStr = [NSString stringWithFormat:@"%@video/videolist_%@_%@_",s,num,Id];
    
    return lastStr;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
